package cn.bugstack.test;

import cn.bugstack.domain.auth.adapter.port.ILoginPort;
import cn.bugstack.domain.order.adapter.event.PaySuccessMessageEvent;
import cn.bugstack.infrastructure.event.EventPublisher;
import cn.bugstack.types.event.BaseEvent;
import com.alibaba.fastjson.JSON;
import com.google.common.eventbus.EventBus;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.util.concurrent.CountDownLatch;
import cn.bugstack.trigger.http.WeixinPortalController;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApiTest {

    @Resource
    private EventBus eventBus;

    @Resource
    private PaySuccessMessageEvent paySuccessMessageEvent;
    @Resource
    private EventPublisher eventPublisher;

    @Value("${spring.rabbitmq.config.producer.topic_order_pay_success.routing_key}")
    private String TOPIC_ORDER_PAY_SUCCESS;

    @Test
    public void test_eventPublisher() throws InterruptedException {
        BaseEvent.EventMessage<PaySuccessMessageEvent.PaySuccessMessage> paySuccessMessageEventMessage = paySuccessMessageEvent.buildEventMessage(
                PaySuccessMessageEvent.PaySuccessMessage.builder()
                        .tradeNo("1100000111")
                        .build());

        eventPublisher.publish(TOPIC_ORDER_PAY_SUCCESS, JSON.toJSONString(paySuccessMessageEventMessage.getData()));

        new CountDownLatch(1).await();
    }

    @Test
    public void test() throws InterruptedException {

        BaseEvent.EventMessage<PaySuccessMessageEvent.PaySuccessMessage> paySuccessMessageEventMessage = paySuccessMessageEvent.buildEventMessage(
                PaySuccessMessageEvent.PaySuccessMessage.builder()
                        .tradeNo("1100000111")
                        .build());

        eventBus.post(JSON.toJSONString(paySuccessMessageEventMessage.getData()));
        log.info("测试完成");
        new CountDownLatch(1).await();
    }

    @Resource
    private ILoginPort loginPort;

    @Test
    public void notifyUserDelivery(String openid, String orderNo) {
        Map<String, Object> data = new HashMap<>();
        Map<String, String> first = new HashMap<>();
        first.put("value", "您的订单已发货");
        first.put("color", "#173177");
        data.put("first", first);

        Map<String, String> keyword1 = new HashMap<>();
        keyword1.put("value", orderNo);
        keyword1.put("color", "#173177");
        data.put("keyword1", keyword1);

        Map<String, String> keyword2 = new HashMap<>();
        keyword2.put("value", "2025-08-25");
        keyword2.put("color", "#173177");
        data.put("keyword2", keyword2);

        Map<String, String> remark = new HashMap<>();
        remark.put("value", "感谢您的购买！");
        remark.put("color", "#173177");
        data.put("remark", remark);


        try {
            loginPort.sendTemplateMessage(openid, data);
        } catch (IOException e) {
            log.error("主动推送消息失败");
            throw new RuntimeException(e);
        }
    }
}
